package com.school.campus.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.school.common.annotation.Log;
import com.school.common.core.controller.BaseController;
import com.school.common.core.domain.AjaxResult;
import com.school.common.enums.BusinessType;
import com.school.campus.domain.StudyRoom;
import com.school.campus.service.IStudyRoomService;
import com.school.common.utils.poi.ExcelUtil;
import com.school.common.core.page.TableDataInfo;

/**
 * 自习室信息Controller
 *
 * @author wwj
 * @date 2025-10-31
 */
@RestController
@RequestMapping("/campus/room")
public class StudyRoomController extends BaseController {
    @Autowired
    private IStudyRoomService studyRoomService;

    /**
     * 查询自习室信息列表
     */
    @PreAuthorize("@ss.hasPermi('campus:room:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudyRoom studyRoom) {
        startPage();
        List<StudyRoom> list = studyRoomService.selectStudyRoomList(studyRoom);
        return getDataTable(list);
    }

    /**
     * 导出自习室信息列表
     */
    @PreAuthorize("@ss.hasPermi('campus:room:export')")
    @Log(title = "自习室信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudyRoom studyRoom) {
        List<StudyRoom> list = studyRoomService.selectStudyRoomList(studyRoom);
        ExcelUtil<StudyRoom> util = new ExcelUtil<StudyRoom>(StudyRoom.class);
        util.exportExcel(response, list, "自习室信息数据");
    }

    /**
     * 获取自习室信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:room:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(studyRoomService.selectStudyRoomById(id));
    }

    /**
     * 新增自习室信息
     */
    @PreAuthorize("@ss.hasPermi('campus:room:add')")
    @Log(title = "自习室信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudyRoom studyRoom) {
        return toAjax(studyRoomService.insertStudyRoom(studyRoom));
    }

    /**
     * 修改自习室信息
     */
    @PreAuthorize("@ss.hasPermi('campus:room:edit')")
    @Log(title = "自习室信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudyRoom studyRoom) {
        return toAjax(studyRoomService.updateStudyRoom(studyRoom));
    }

    /**
     * 删除自习室信息
     */
    @PreAuthorize("@ss.hasPermi('campus:room:remove')")
    @Log(title = "自习室信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(studyRoomService.deleteStudyRoomByIds(ids));
    }

    /**
     * 获取自习室信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:room:query')")
    @GetMapping(value = "/{id}/detail")
    public AjaxResult getRoomDetailWithSeats(@PathVariable("id") Long id) {
        return success(studyRoomService.selectStudyRoomVOById(id));
    }
}
